export default {
    props:{
        mTime:{
            type:Number,
            default:60*60*1000
        },
        autoPlay:{
            type:Boolean,
            default:true
        },
        format:{
            type:String,
            default:'colon'
        },
        speed:{
            type:Number,
            default:1000
        }

    },
    data: {
        timeDate:{
            second:0,
            hour:0,
            minute:0,
            day:0
        },
        typeArr:['second','minute','hour']

    },
    computed:{
        countDownFormat(){
            let content=''
            if(this.format=='colon'){
                content=this.zeorPadding(this.timeDate.hour)+':'
                +this.zeorPadding(this.timeDate.minute)+':'+
                this.zeorPadding(this.timeDate.second)
            }else if(this.format=='day'){
                content=this.zeorPadding(this.timeDate.day)+'天'+
                this.zeorPadding(this.timeDate.hour)+'时'
                +this.zeorPadding(this.timeDate.minute)+'分'+
                this.zeorPadding(this.timeDate.second)+'秒'
            }
            return content;
        },
        transionTime(){
            return {
                'hour':this.zeorPadding(this.timeDate.hour),
                'second':this.zeorPadding(this.timeDate.second),
                'minute':this.zeorPadding(this.timeDate.minute)
            }
        }
    },
    onInit() {
        if((''+this.mTime).length>21) return;
        this.$watch('format','initTime');
        this.formatDuring();
        this.autoPlay && this.start();
        if(this.format=='day'){
        this.typeArr.push('day');
        }
        setTimeout(()=>{
            this.$emit('transmission',this.transionTime);
        })
    },
    onShow(){
    },
    initTime(val){
        if(val){
            this.formatDuring();
        }
    },
    formatDuring(){
        let oHour=parseInt(parseInt(this.mTime)/(60*60));
        if(this.format==='day'){
            this.timeDate.day=oHour>=24?Math.floor(oHour/24):0;
            this.timeDate.hour=this.timeDate.day>0?oHour-this.timeDate.day*24:oHour;
        }else{
            this.timeDate.hour=oHour;
        }
        this.timeDate.minute=parseInt((parseInt(this.mTime)-oHour*60*60)/60);
        this.timeDate.second=parseInt((parseInt(this.mTime)-oHour*60*60-this.timeDate.minute*60));
    },
    zeorPadding(data){
        return (typeof data).toString() =="number"
               && data<10?'0'+data:data;
    },
    start(){
        if(this.countDown!=null) return;
        this.countDown=setInterval(this.countDownTime,this.speed);
    },
    stop(){
        clearInterval(this.countDown);
        this.countDown=null;
    },
    reset(){
        this.stop();
        this.formatDuring();
        this.$emit('transmission', this.transionTime);
        this.autoPlay && this.start();
    },
    countDownTime(){
        this.timeDate.second!=0 && this.timeDate.second--;
        this.changeTime(0);
        this.$emit('transmission', this.transionTime);
    },
    countDownFinish(){
        this.stop();
        this.$emit('finishEvent',{msg:'倒计时完事了'});
    },
    changeTime(num){
        if(this.timeDate[`${this.typeArr[num]}`]===0){
           let isStop= this.typeArr.every((item)=>{
                return this.timeDate[`${item}`]===0
            })
           if(isStop) return this.countDownFinish() ;
           this.changeTime(++num);
        }else{
            if(num==0) return ;
            this.timeDate.second=59;
            for (let index = 2; index <= num; index++) {
                if(num-index>=0){
                    this.timeDate[`${this.typeArr[num-index+1]}`]=59;
                }
            }
            this.timeDate[`${this.typeArr[num]}`]-=1;
        }
    }
}
